<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="病症名称" prop="illnessName">
        <el-input
          v-model="queryParams.illnessName"
          placeholder="请输入病症名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="简介" prop="synopsis">
        <el-input
          v-model="queryParams.synopsis"
          placeholder="请输入简介"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="咨询价格" prop="price">
        <el-input
          v-model="queryParams.price"
          placeholder="请输入咨询价格"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-upload"
          size="mini"
          @click="handleImport"
        >导入
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-delete"
          size="mini"
          @click="deleteDiseaseAll"
        >删除全部病种
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="diseaseList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="病症id" align="center" prop="diseaseId" /> -->
      <el-table-column label="病症名称" align="center" prop="illnessName" />
<!--      <el-table-column label="图片" align="center" prop="image" width="100">-->
<!--        <template slot-scope="scope">-->
<!--          <image-preview :src="scope.row.image" :width="50" :height="50"/>-->
<!--        </template>-->
<!--      </el-table-column>-->
      <el-table-column label="科室" align="center">
        <template slot-scope="scope">
          {{ getDepartmentName(scope.row.departmentId) }}
        </template>
      </el-table-column>
      <!-- <el-table-column label="简介" align="center" prop="synopsis" /> -->
<!--      <el-table-column label="科室" align="center" prop="departmentName" />-->
      <el-table-column label="咨询价格" align="center" prop="price" />
<!--      <el-table-column label="排序" align="center" prop="sort" />-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-view"-->
<!--            @click="handleSelect(scope.row)"-->
<!--            v-hasPermi="['system:disease:edit']"-->
<!--          >查看</el-button>-->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改【请填写功能名称】对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px" :disabled="title == '查看【病种】'">
        <el-form-item label="病症名称" prop="illnessName">
          <el-input v-model="form.illnessName" placeholder="请输入病症名称" />
        </el-form-item>
<!--        <el-form-item label="图片" prop="image">-->
<!--          <image-upload v-model="form.image"/>-->
<!--          <div class="el-upload__tip">建议尺寸：高100rpx×宽100rpx，支持 JPG/PNG 格式</div>-->
<!--        </el-form-item>-->
<!--        <el-form-item label="简介" prop="synopsis">-->
<!--          <el-input v-model="form.synopsis" placeholder="请输入简介" />-->
<!--        </el-form-item>-->
        <el-form-item label="咨询价格" prop="price">
          <el-input v-model="form.price" placeholder="请输入咨询价格" />
        </el-form-item>
        <!-- ✅ 新增：科室下拉 -->
        <el-form-item label="所属科室" prop="departmentId">
          <el-select v-model="form.departmentId" placeholder="请选择科室" style="width: 100%">
            <el-option
              v-for="item in departmentOptions"
              :key="item.departmentId"
              :label="item.departmentType"
              :value="item.departmentId"
            />
          </el-select>
        </el-form-item>
<!--        <el-form-item label="请输入排序数字" prop="sort">-->
<!--          <el-input v-model="form.sort" placeholder="请输入排序数字" />-->
<!--        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer" v-if="title != '查看【病种】'">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <upload-excel
      ref="importExcel"
      action="/dev-api/system/disease/addFile"
      :template-url="'/system/disease/importTemplate'"
      @success="getList"
    />
  </div>
</template>

<script>
import { listDisease, getDisease, delDisease, addDisease, updateDisease, deleteDiseaseAll } from "@/api/disease/disease"
import { getListDep, listDepartment } from "@/api/department/department"
import UploadExcel from "@/components/UploadExcel/index.vue";

export default {
  name: "Disease",
  components: {UploadExcel},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 【请填写功能名称】表格数据
      diseaseList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        illnessName: null,
        synopsis: null,
        price: null,
      },
      departmentOptions: [],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        illnessName: [
          { required: true, message: '请输入病症名称', trigger: 'blur' }
        ],
        // image: [
        //   { required: true, message: '请上传图片', trigger: 'change' }
        // ],
        // synopsis: [
        //   { required: true, message: '请输入简介', trigger: 'blur' }
        // ],
        price: [
          { required: true, message: '请输入咨询价格', trigger: 'blur' }
        ],
        departmentId: [
          { required: true, message: '请选择科室', trigger: 'change' }
        ]
      }

    }
  },
  created() {
    this.getList()
  },
  methods: {
    handleImport() {
      this.$refs.importExcel.openDialog()
    },
    getDepartmentName(id) {
      const dept = this.departmentOptions.find(d => d.departmentId === id)
      return dept ? dept.departmentType : '未知科室'
    },
    /** 查询【请填写功能名称】列表 */
    getList() {
      this.loading = true
      getListDep().then(response => {
        this.departmentOptions = response
      })
      listDisease(this.queryParams).then(response => {
        this.diseaseList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        diseaseId: null,
        illnessName: null,
        image: null,
        synopsis: null,
        price: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null,
        departmentId: null
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.diseaseId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "添加【病种】"
      getListDep().then(response => {
        this.departmentOptions = response
      })
    },
    deleteDiseaseAll() {
      deleteDiseaseAll().then(response => {
        this.$modal.msgSuccess("删除成功")
        this.getList()
      })
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      // this.reset()
      console.log(row)
      getListDep().then(response => {
        this.departmentOptions = response
      })
      const diseaseId = row.diseaseId || this.ids
      getDisease(diseaseId).then(response => {
        this.form = response.data
        this.open = true
        this.title = "修改【病种】"
      })
    },
    handleSelect(row){
      getListDep().then(response => {
        this.departmentOptions = response
      })
      const diseaseId = row.diseaseId || this.ids
      getDisease(diseaseId).then(response => {
        this.form = response.data
        this.open = true
        this.title = "查看【病种】"
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.diseaseId != null) {
            updateDisease(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addDisease(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const diseaseIds = row.diseaseId || this.ids
      this.$modal.confirm('是否确认删除【病种】编号为"' + diseaseIds + '"的数据项？').then(function() {
        return delDisease(diseaseIds)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/disease/export', {
        ...this.queryParams
      }, `disease_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
